package server.modules.generator.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import server.modules.generator.entity.PointDto;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import server.modules.generator.entity.PointEntity;
import server.modules.generator.service.PointService;
import server.common.utils.PageUtils;
import server.common.utils.R;
import server.common.utils.PageUtils;
import server.common.utils.R;
import server.modules.generator.entity.PointDto;
import server.modules.generator.entity.PointEntity;
import server.modules.generator.service.PointService;


/**
 *
 * @author
 * @email
 * @date 2021-04-16 23:05:23
 */
@RestController
@RequestMapping("generator/point")
public class PointController {
    @Autowired
    private PointService pointService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("generator:point:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = pointService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 下拉列表
     */
    @GetMapping("/listAll")
    public R listAll() {
        List<PointEntity> res = pointService.list();
        List<PointDto> result = new ArrayList<>();
        for (PointEntity singleData : res) {
            PointDto dto = new PointDto();
            dto.setLabel(singleData.getName());
            dto.setValue(String.valueOf(singleData.getLongitude()) + "," + singleData.getLatitude());

            result.add(dto);
        }

        return R.ok().put("list", result);
    }


    @PostMapping("/checkValue")
    public R checkValue(@RequestBody Object req) {
        String result = "";
        Wrapper<PointEntity> queryWrapper = new QueryWrapper<PointEntity>();
        List<PointEntity> res = pointService.list(queryWrapper);
        List<PointEntity> removePoints = new ArrayList<>();

        Map map = (Map) req;
        List routes = (List) map.get("routes");
        List<Integer> nums = new ArrayList<>();
//        List<Map> steps = (List) ((Map) routes.get(0)).get("steps");


        for (Object route : routes) {
            int num = 0;
            List<Map> steps = (List) ((Map) route).get("steps");
            res = pointService.list(queryWrapper);

            for (Map step : steps) {
                if (res.isEmpty()) break;
                List<Map> paths = (List) step.get("path");
                for (Map path : paths) {
                    if (res.isEmpty()) break;
                    double lng = (Double) path.get("lng");
                    double lat = (Double) path.get("lat");

                    for (PointEntity point : res) {
                        if (((Math.abs(lng - point.getLongitude()) < 0.00005) && (Math.abs(lat - point.getLatitude()) < 0.0001)) ||
                                ((Math.abs(lng - point.getLongitude()) < 0.0001) && (Math.abs(lat - point.getLatitude()) < 0.00005))) {
                            removePoints.add(point);
                        }
                    }
                    if (!removePoints.isEmpty()) {
                        res.removeAll(removePoints);
                        removePoints.clear();
                    }

                }
            }
            nums.add(num);

        }

        return R.ok().put("num", nums.toString());
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    // @RequiresPermissions("generator:point:info")
    public R info(@PathVariable("id") Integer id) {
        PointEntity point = pointService.getById(id);

        return R.ok().put("point", point);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("generator:point:save")
    public R save(@RequestBody PointEntity point) {
        pointService.save(point);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("generator:point:update")
    public R update(@RequestBody PointEntity point) {
        pointService.updateById(point);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("generator:point:delete")
    public R delete(@RequestBody Integer[] ids) {
        pointService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
